package com.xinqi.modules.course.course.convert;

import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.core.enums.ClassRoleEnum;
import com.xinqi.common.mapstruct.CommonConvertConfig;
import com.xinqi.common.security.utils.UserHelp;
import com.xinqi.modules.course.clazz.service.ClassService;
import com.xinqi.modules.course.clazz.dto.rsp.ClassResultDTO;
import com.xinqi.modules.course.course.dto.req.CourseCreateDTO;
import com.xinqi.modules.course.course.dto.req.CourseCreateNewDTO;
import com.xinqi.modules.course.course.dto.req.CourseUpdateDTO;
import com.xinqi.modules.course.course.dto.req.CourseUpdateNewDTO;
import com.xinqi.modules.course.course.dto.rsp.CourseResultDTO;
import com.xinqi.modules.course.course.dto.rsp.CourseResultNewDTO;
import com.xinqi.modules.course.course.domain.CourseEntity;
import com.xinqi.modules.course.course.enums.CourseArchiveStatusEnum;
import com.xinqi.modules.course.course.enums.CourseStatusEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 课程表转换
 *
 * @author: system
 * @date: 2023/01/17
 */
@Mapper(config = CommonConvertConfig.class)
public abstract class CourseConvert {

    @Autowired
    private ClassService classService;

    public abstract CourseEntity create(CourseCreateDTO s);

    public abstract CourseEntity create(CourseCreateNewDTO s);

    public abstract CourseEntity update(CourseUpdateDTO s, @MappingTarget CourseEntity t);

    public abstract CourseEntity update(CourseUpdateDTO s);

    public abstract CourseEntity update(CourseUpdateNewDTO s);

    public abstract CourseResultDTO convert(CourseEntity s);

    public abstract CourseResultNewDTO convert(CourseResultDTO s);

    public abstract CourseEntity copy(CourseResultDTO s);

    /**
     * 只有老师可以恢复归档全班
     *
     * @param dto
     */
    public void renderArchived(CourseResultDTO dto) {
        List<ClassResultDTO> clasList = classService.findArchivedClassAndRoleByCourseIdAndStudentId(dto.getId(),
                UserHelp.userId(), YesNoEnum.YES.ordinal());
        dto.setClassList(clasList);
        //助教不能归档全班
        if (Objects.equals(CourseArchiveStatusEnum.ARCHIVE_ALL.getCode(), dto.getArchive())) {
            List<ClassResultDTO> teacherRoleList = clasList.stream().filter(c -> Objects.equals(ClassRoleEnum.TEACHER.getCode(), c.getClassRole())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(teacherRoleList)) {
                dto.setIsHaveRecoverAllPermission(YesNoEnum.YES.ordinal());
            } else {
                dto.setIsHaveRecoverAllPermission(YesNoEnum.NO.ordinal());
            }
        }
    }

    public void renderJoined(CourseResultDTO dto) {
        List<String> classRoles = new ArrayList<>();
        classRoles.add(ClassRoleEnum.REPRESENTATIVE.getCode());
        classRoles.add(ClassRoleEnum.STUDENT.getCode());
        List<ClassResultDTO> clasList = classService.findByCourseIdAndRoles(dto.getId(), null, UserHelp.userId(), classRoles, YesNoEnum.NO.ordinal());
        dto.setClassList(clasList);
    }

    public void renderCreated(CourseResultDTO dto) {
        List<ClassResultDTO> clasList = classService.findByCourseIdAndIsTeacher(dto.getId(), null, UserHelp.userId(), YesNoEnum.YES.ordinal(), YesNoEnum.NO.ordinal());
        dto.setClassList(clasList);
    }

    @AfterMapping
    public void after(CourseCreateDTO s, @MappingTarget CourseEntity t) {
        t.setStatus(CourseStatusEnum.NORMAL);
    }


}
